package org.market.domain.strategy.service.rule.tree.impl;

import lombok.extern.slf4j.Slf4j;
import org.market.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import org.market.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import org.market.domain.strategy.repository.IStrategyRepository;
import org.market.domain.strategy.service.armory.IStrategyDispatch;
import org.market.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.market.domain.strategy.service.rule.tree.IRuleTreeNode;
import org.market.domain.strategy.service.rule.tree.factory.DefaultRuleTreeFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/25 下午10:05
 * @Version: 1.0
 */
@Slf4j
@Service
@Component("rule_stock")
public class RuleTreeStockNode implements IRuleTreeNode {
    @Resource
    private IStrategyDispatch strategyDispatch;
    @Resource
    private IStrategyRepository repository;
    @Override
    public DefaultRuleTreeFactory.RuleActionEntity logic(String userId, Long strategyId, Integer awardId,String ruleValue) {
        log.info("规则过滤-库存扣减 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
        Boolean status = strategyDispatch.subtractAwardCount(strategyId, awardId);

        if (status){
            log.info("规则过滤-库存扣减-成功 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
            repository.awardStockConsumeSendQueue(StrategyAwardStockKeyVO.builder()
                    .strategyId(strategyId)
                    .awardId(awardId)
                    .build());

            log.info("库存扣减节点,返回信息:userId:{},strategyId:{},awardId:{}",userId,strategyId,awardId);
            return DefaultRuleTreeFactory.RuleActionEntity
                    .builder()
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                    .strategyAwardVO(DefaultRuleTreeFactory.StrategyAwardVO.builder()
                            .awardId(awardId)
                            .awardRuleValue(ruleValue)
                            .build())
                    .build();
        }
        log.info("ruleStock----->ruleLuck");
        return DefaultRuleTreeFactory.RuleActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.ALLOW)
                .build();
    }
}
